package com.lims.common;

import lombok.Data;
import org.slf4j.MDC;

import java.io.Serializable;

@Data
public class BaseResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private T data;

    private Integer code;

    private String msg;

    private String traceId;

    public BaseResult(){ }

    public BaseResult(Integer code, String msg){
        this.code = code;
        this.msg = msg;
        this.traceId = MDC.get(Conts.TRACE_ID);
    }

    public BaseResult(T data, Integer code, String msg) {
        this.data = data;
        this.code = code;
        this.msg = msg;
        this.traceId = MDC.get(Conts.TRACE_ID);
    }

    public static <T> boolean isSuccess(BaseResult<T> result){
        if(null == result || null == result.getCode()){
            return false;
        }
        return result.getCode().equals(0);
    }

    public static <T> BaseResult<T> success() {
        BaseResult<T> result = new BaseResult();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        result.setTraceId(MDC.get(Conts.TRACE_ID));
        return result;
    }

    public static <T> BaseResult<T> success(T data) {
        BaseResult<T> result = new BaseResult();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        result.setData(data);
        result.setTraceId(MDC.get(Conts.TRACE_ID));
        return result;
    }

    public static <T> BaseResult<T> failed(Integer code, String msg) {
        BaseResult<T> result = new BaseResult();
        result.setCode(code);
        result.setMsg(msg);
        result.setTraceId(MDC.get(Conts.TRACE_ID));
        return result;
    }

    public static <T> BaseResult<T> failed(String msg) {
        BaseResult<T> result = new BaseResult();
        result.setCode(ResultCode.COMM_ERROR.getCode());
        result.setMsg(msg);
        result.setTraceId(MDC.get(Conts.TRACE_ID));
        return result;
    }

    public static <T> BaseResult<T> failed(ResultCode resultCode) {
        BaseResult<T> result = new BaseResult();
        result.setCode(resultCode.getCode());
        result.setMsg(resultCode.getMsg());
        result.setTraceId(MDC.get(Conts.TRACE_ID));
        return result;
    }
}
